package com.dgut.controller;

import com.dgut.vo.ResultVO;
import com.github.pagehelper.PageInfo;
import com.dgut.vo.QueryVO;
import com.dgut.pojo.Student;
import com.dgut.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("student")
@ResponseBody
public class StudentController {
    @Resource
    private StudentService studentService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVO<Student> queryByPage(Integer pageNum, Integer pageSize, QueryVO vo) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        PageInfo<Student> info = studentService.queryByPage(pageNum, pageSize, vo);
        return new ResultVO<>(info);
    }
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResultVO<Student> queryById(@PathVariable("id") Integer stuId) {
        Student student = studentService.queryById(stuId);
        return new ResultVO<>(student);
    }
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVO<Student> addStudent(Student student) {
        System.out.println("学生信息："+student);
        int i = studentService.addStudent(student);
        if (i == 1) {
            return new ResultVO<>();
        } else {
            return new ResultVO<>(500, "添加失败，服务器内部错误，请稍后再试");
        }
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public ResultVO<Student> deleteStudent(@PathVariable("id") Integer stuId) {
        int i = studentService.deleteStudent(stuId);
        if (i == 1) {
            return new ResultVO<>();
        } else {
            return new ResultVO<>(500, "删除失败，服务器内部错误，请稍后再试");
        }
    }

    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ResultVO<Student> updateStudent(Student student) {
        System.out.println("更新学生信息---"+student);
        int i = studentService.updateStudent(student);
        if (i == 1) {
            return new ResultVO<>();
        } else {
            return new ResultVO<>(500, "修改失败，服务器内部错误，请稍后再试");
        }
    }
}
